<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <a-button preIcon="ant-design:plus-outlined" type="primary" @click="handleAdd">新增</a-button>
        <a-dropdown v-if="getSelectRowKeys().length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="i-ant-design:delete-outlined" />
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button
            >批量操作
            <Icon style="fontsize: 12px" icon="ant-design:down-outlined" />
          </a-button>
        </a-dropdown>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getActions(record)" />
      </template>
    </BasicTable>
    <ArticleModal @register="registerModal" @success="reload" />
  </div>
</template>
<script lang="ts" name="ArticleList" setup>
  import { ref, onMounted } from "vue"
  import { BasicTable, TableAction, useTable } from "/@/components/Table"
  import { useModal } from "/@/components/Modal"
  import { Icon } from "/@/components/Icon"
  import ArticleModal from "./ArticleModal.vue"
  import { useGlobSetting } from "/@/hooks/setting"
  import { columns, searchFormSchema } from "./article.data"
  import { pageArticle, deleteArticle, batchDeleteArticle } from "/@/api/sys/article"
  import { listGroup } from "/@/api/sys/article"
  import { useListPage } from "/@/hooks/system/useListPage"
  import { useWangEditor } from "/@/components/My/WangEditor"
  useWangEditor()
  const glob = useGlobSetting()
  const [registerModal, { openModal }] = useModal()
  const [registerTable, { reload, getForm, getSelectRowKeys }] = useTable({
    title: "文章列表",
    api: pageArticle,
    immediate: false,
    bordered: false,
    actionColumn: {
      width: 120,
      title: "操作",
      dataIndex: "action",
      fixed: "right",
      slots: { customRender: "action" },
    },
    columns: columns,
    useSearchForm: true,
    formConfig: {
      baseColProps: { lg: 6, sm: 12, md: 12 },
      wrapperCol: { lg: 24, md: 24 },
      labelWidth: 90,
      schemas: searchFormSchema,
    },
  })

  onMounted(async () => {
    const { updateSchema, setFieldsValue } = getForm()
    reload()
    updateSchema([
      {
        field: "groupId",
        componentProps: ({ formModel, formActionType }) => {
          const { setFieldsValue } = formActionType
          return {
            optionFilterProp: "label",
            showSearch: true,
            labelField: "name",
            valueField: "id",
            api: listGroup,
            onSelect: (value: any, option: any) => {
              console.log("option", option)
              setFieldsValue({ groupId: option.value })
              reload()
            },
          }
        },
        colProps: { span: 8 },
      },
    ])
  })
  function handleAdd() {
    openModal(true, {
      isUpdate: false,
    })
  }

  /**
   * 编辑事件
   */
  function handleEdit(record) {
    openModal(true, {
      record,
      isUpdate: true,
    })
  }

  async function handleDelete(record) {
    await deleteArticle({ id: record.id }, reload)
  }

  async function batchHandleDelete() {
    await batchDeleteArticle({ ids: getSelectRowKeys() }, reload)
  }

  function getActions(record) {
    return [
      {
        label: "编辑",
        onClick: handleEdit.bind(null, record),
        // ifShow: record.sendStatus == 0
      },
      {
        label: "删除",
        color: "error",
        ifShow: record.sendStatus != 1,
        popConfirm: {
          title: "是否确认删除",
          confirm: handleDelete.bind(null, record),
        },
      },
    ]
  }
</script>
